package com.applcn.shop.threadlocal;

/**
 * com.applcn.student.web.admin.dto.request
 * Description:
 *
 * @author: 大妖怪
 * @date: 2019-12-14 19:14
 */
public class ShopRequestContextHolder {

    /**
     * 用户信息上下文内存变量
     */
    private final static ThreadLocal<ShopRequestContext> MALL_REQUEST_CONTEXT_THREAD_LOCAL = new ThreadLocal<>();

    private ShopRequestContextHolder() {
    }

    /**
     * 初始化当前请求用户上下文
     *
     * @param shopRequestContext
     */
    public static void setUserRequestContext(ShopRequestContext shopRequestContext) {
        MALL_REQUEST_CONTEXT_THREAD_LOCAL.set(shopRequestContext);
    }

    /**
     * 初始化当前请求用户上下文
     *
     * @param userId
     */
    public static void setUserRequestContext(Long userId) {
        ShopRequestContext shopRequestContext = getLocalRequestContext();
        if (shopRequestContext == null) {
            shopRequestContext = new ShopRequestContext();
        }
        shopRequestContext.setUserId(userId);
        MALL_REQUEST_CONTEXT_THREAD_LOCAL.set(shopRequestContext);
    }

    /**
     * 返回用户id
     */
    public static Long getUserId() {
        ShopRequestContext shopRequestContext = getLocalRequestContext();
        if (shopRequestContext == null) {
            return null;
        }
        if (shopRequestContext.getUserId() == null) {
            return null;
        }
        return shopRequestContext.getUserId();
    }

    /**
     * 初始化商户上下文
     *
     * @param merchantId
     */
    public static void setMerchantRequestContext(Long merchantId) {
        ShopRequestContext shopRequestContext = getLocalRequestContext();
        if (shopRequestContext == null) {
            shopRequestContext = new ShopRequestContext();
        }
        shopRequestContext.setMerchantId(merchantId);
        MALL_REQUEST_CONTEXT_THREAD_LOCAL.set(shopRequestContext);
    }

    /**
     * 返回商户id
     */
    public static Long getMerchantId() {
        ShopRequestContext shopRequestContext = getLocalRequestContext();
        if (shopRequestContext == null) {
            return null;
        }
        if (shopRequestContext.getMerchantId() == null) {
            return null;
        }
        return shopRequestContext.getMerchantId();
    }

    /**
     * 初始化店铺上下文
     *
     * @param shopId
     */
    public static void setShopRequestContext(Long shopId) {
        ShopRequestContext shopRequestContext = getLocalRequestContext();
        if (shopRequestContext == null) {
            shopRequestContext = new ShopRequestContext();
        }
        shopRequestContext.setShopId(shopId);
        MALL_REQUEST_CONTEXT_THREAD_LOCAL.set(shopRequestContext);
    }

    /**
     * 返回店铺id
     */
    public static Long getShopId() {
        ShopRequestContext shopRequestContext = getLocalRequestContext();
        if (shopRequestContext == null) {
            return null;
        }
        if (shopRequestContext.getShopId() == null) {
            return null;
        }
        return shopRequestContext.getShopId();
    }

    /**
     * 获取当前请求的上下文
     *
     * @return
     */
    public static ShopRequestContext getLocalRequestContext() {
        return MALL_REQUEST_CONTEXT_THREAD_LOCAL.get();
    }

    /**
     * 清除当前线程对请求上下文对象的引用（即让GC回收当前请求上下文对象）
     */
    public static void destroyUserRequestContext() {
        MALL_REQUEST_CONTEXT_THREAD_LOCAL.remove();
    }
}
